package com.rmdjzz.slmmn.controller.admin.singerManagement;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rmdjzz.slmmn.pojo.base.Singer;
import com.rmdjzz.slmmn.pojo.result.Result;
import com.rmdjzz.slmmn.pojo.vo.PageVo;
import com.rmdjzz.slmmn.service.SingerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.UUID;

/**
 * @作者 lzy
 * @创建时间 2023/5/29
 */
@Controller
@Api(tags = "歌手管理")
@RequestMapping("/singer")
@Slf4j
public class SingerManagement {

    @Autowired
    private SingerService singerService ;

    @PostMapping ("/addSinger")
    @ApiOperation("添加歌手")
    @ResponseBody
    public Result addSinger(Singer singer, MultipartFile mulHeaderImg) {
        log.info("Singer:"+singer);
        //歌曲海报设置规则：文件名+uuid+png---存放在虚拟路径song文件下
        try {
            //获取输入流
            InputStream inputStream = mulHeaderImg.getInputStream();
            //获取输出流
            //准备待写入的文件
            String s = UUID.randomUUID().toString();
            String url="C:\\slmmn\\singer\\"+mulHeaderImg.getName()+s+".png";
            writeFile(inputStream, url);
            //设置歌曲类的海报存放路径
            singer.setHeaderImg("/singer/"+mulHeaderImg.getName()+s+".png");
        } catch (IOException e) {
            e.printStackTrace();
        }

       singerService.save(singer);
    return Result.ok();
    }

   @DeleteMapping("/deleteSinger/{id}")
   @ApiOperation("删除歌手")
   @ResponseBody
    public  Result  deleteSinger(@PathVariable Integer id){
        singerService.removeById(id);
        return  Result.ok();
   }

    @GetMapping("/searchSinger")
    @ApiOperation("查询歌手信息并分页")
    @ResponseBody
    public  Result  searchSinger(PageVo pageVo){
        QueryWrapper<Singer> singerQueryWrapper = new QueryWrapper<>();
        Page objectPage = new Page<>(pageVo.getStartIndex(),pageVo.getSize());
        if (!pageVo.getStr().equals("null") && !pageVo.getStr().equals("")){
            singerQueryWrapper.like("singer_name",pageVo.getStr());
            Page singerPage = singerService.page(objectPage,singerQueryWrapper);
            return Result.ok(singerPage);
        }else{
            Page singerPage = singerService.page(objectPage,singerQueryWrapper);
            return Result.ok(singerPage);
        }
    }


    private void writeFile(InputStream inputStream, String url) throws IOException {
        File target = new File(url);
        //获取输出流
        FileOutputStream fos = new FileOutputStream(target);
        //获取包装类
        BufferedInputStream bis = new BufferedInputStream(inputStream);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        //写文件
        int ch = 0;
        while ((ch = bis.read()) != -1) {
            bos.write(ch);
        }
        // 关流
        bos.close();
        bis.close();
    }

}
